package com.minivision.sdktestdemo.theme.utils;

import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.minivision.sdktestdemo.R;

/**
 * Created by Administrator on 2016/6/22.
 */
public class ViewAttributeUtils {
    public static int getAttributeValue(AttributeSet set, int paramInt) {
        int value = -1;
        int count = set.getAttributeCount();
        for (int i = 0; i < count; ++i) {
            if (set.getAttributeNameResource(i) == paramInt) {
                String attr = set.getAttributeValue(i);
                if (null != attr && attr.startsWith("?")) {
                    value = Integer.valueOf(attr.substring(1)).intValue();
                    return value;
                }
            }
        }
        return value;
    }

    public static int getBackgroundAttribute(AttributeSet attr) {
        return getAttributeValue(attr, android.R.attr.background);
    }

    public static int getTextColorAttribute(AttributeSet attr) {
        return getAttributeValue(attr, android.R.attr.textColor);
    }

    public static int getAccentColorAttribute(AttributeSet attr) {
        return getAttributeValue(attr, android.R.attr.color);
    }

    public static int getTintColorAttribute(AttributeSet attr) {
        return getAttributeValue(attr, android.R.attr.tint);
    }

    public static int getToolbarTitleColorAttribute(AttributeSet attr) {
        return getAttributeValue(attr, R.attr.toolbar_text_color);
    }

    public static void applyBackgroundDrawable(CustomThemeInterface ci, Resources.Theme theme, int paramInt) {
        TypedArray ta = theme.obtainStyledAttributes(new int[]{paramInt});
        Drawable drawable = ta.getDrawable(0);
        if (null != ci) {
            (ci.getView()).setBackgroundDrawable(drawable);
        }
        ta.recycle();
    }

    public static void applyTextColor(CustomThemeInterface ci, Resources.Theme theme, int paramInt) {
        TypedArray ta = theme.obtainStyledAttributes(new int[]{paramInt});
        int resourceId = ta.getColor(0, 0);
        if (null != ci && ci instanceof TextView) {
            ((TextView) ci.getView()).setTextColor(resourceId);
        }
        ta.recycle();
    }

    public static void applySwitchColor(CustomThemeInterface ci, Resources.Theme theme, int paramInt) {
        throw new RuntimeException("功能不完善");
        //功能不完善
//        TypedArray ta = theme.obtainStyledAttributes(new int[]{paramInt});
//        int resourceId = ta.getColor(0, 0);
//        if (null != ci && ci instanceof Switch) {
//            ((Switch)ci.getView()).getThumbDrawable().setColorFilter(paramInt, PorterDuff.Mode.MULTIPLY);
//            ((Switch)ci.getView()).getThumbDrawable().setColorFilter(paramInt, PorterDuff.Mode.MULTIPLY);
//        }
    }

    public static void applyTintColor(CustomThemeInterface ci, Resources.Theme theme, int paramInt) {
        TypedArray ta = theme.obtainStyledAttributes(new int[]{paramInt});
        int resourceId = ta.getColor(0, 0);
        if (null != ci && ci instanceof ImageView) {
            ((ImageView) ci.getView()).setColorFilter(resourceId);
        }
    }

    public static boolean isNeedChangeColor(View view) {
        String isNeed = (String) view.getTag();
        if (!ThemeUtils.isDarkTheme() && ThemeUtils.getThemeType() == ThemeUtils.DIY_THEME) {
            if (isNeed != null && isNeed.equals("true")) {
                return true;
            }
        }
        return false;
    }
}
